package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.BussinessDao;
import com.baichang.taoshunda.dao.GoodsCommentDao;
import com.baichang.taoshunda.dao.OrderDao;
import com.baichang.taoshunda.entity.GoodsComment;
import com.baichang.taoshunda.entity.Order;
import com.baichang.taoshunda.service.GoodsCommentService;
import com.baichang.taoshunda.service.OrderService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2017/8/22 0022.
 */
@Service
public class GoodsCommentServiceImpl implements GoodsCommentService {
    @Autowired
    private GoodsCommentDao goodsCommentDao;
    @Autowired
    private OrderService orderService;
    @Autowired
    private BussinessDao bussinessDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * 查询每个商家的用户评论
     * 接口
     * 武圣路
     *
     * @param bussId   商家编号
     * @param nowPage
     * @param pageSize @return
     */
    @Override
    public Map<String, Object> getCommentByBussId(int bussId, int nowPage, int pageSize) {
        Map<String, Object> map = new HashedMap();
//        try {
        int total = goodsCommentDao.getTotal(bussId);
        List<Map<String, Object>> commentList = goodsCommentDao.getCommentByBussId(bussId, nowPage, pageSize);
        List<Map<String, Object>> list = new ArrayList<>();
//        for (int i = 0; commentList.size() > i; i++) {
//            Map<String, Object> commentMap = commentList.get(i);
//            commentMap.put("content", EmojiUtil.emojiRecovery2(commentMap.get("content").toString()));
//            list.add(commentMap);
//        }
        map.put("list", commentList);
        map.put("startNumber", goodsCommentDao.getSartNum(bussId));
        map.put("total", total);
        return map;
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        return map;
    }

    /**
     * 添加评论
     *
     * @param orderNumber    订单编号
     * @param userId         用户编号
     * @param commentContent 评论内容
     * @return
     */
    @Override
    public boolean addGoodsComment(String orderNumber, int userId, int bussId, String commentContent) throws ServiceException {
        JSONArray jsonArray = JSONArray.fromObject(commentContent);
        boolean flag = false;
//        try {
        for (int i = 0; i < jsonArray.size(); i++) {

            JSONObject jsonObject = JSONObject.fromObject(jsonArray.get(i));
            Integer goodsId = Integer.parseInt(jsonObject.get("id").toString());
            Integer score = Integer.parseInt(jsonObject.get("score").toString());
            String spec = jsonObject.get("spec").toString();
            String content = jsonObject.get("content").toString();
            String picture = jsonObject.get("picture").toString();
            GoodsComment goodsComment = new GoodsComment();
            goodsComment.setUserId(userId);
            goodsComment.setCreated(new Date());
            goodsComment.setStarNum(score);
            goodsComment.setGoodsId(goodsId);
            goodsComment.setImages(picture);
            goodsComment.setBusId(bussId);
            goodsComment.setSpecJson(spec);
            goodsComment.setOrderNumber(orderNumber);
//                goodsComment.setContent(EmojiUtil.emojiConvert1(content));
            goodsComment.setContent(content);
            flag = goodsCommentDao.save(goodsComment) > 0;
        }
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        if (flag) {
            Order order = orderDao.getOrderByOrderNumber(orderNumber);
            flag = orderService.updateOrderStateByorderId(order.getId());
        }
        return flag;
    }

    /**
     * 查询订单信息
     *
     * @param orderId
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryOrderComment(Integer orderId, int nowPage, int pageSize) {
        return goodsCommentDao.queryOrderComment(orderId, nowPage, pageSize);
    }

    /**
     * 查询我的所有评论
     *
     * @param userId   用户编号
     * @param nowPage  当前页
     * @param pageSize 条数
     * @return
     */
    @Override
    public List<Map<String, Object>> findCommentByUserId(Integer userId, Integer nowPage, Integer pageSize) {
        return goodsCommentDao.findCommentByUserId(userId, nowPage, pageSize);
    }

    /**
     * 删除评论
     *
     * @param commentId 评论编号
     * @return
     */
    @Override
    public boolean deleteCommentById(Integer commentId) {
        return goodsCommentDao.deleteById(commentId) > 0;
    }

    @Override
    public boolean review(Integer commentId, String review, String reviewImg) {

        return goodsCommentDao.review(commentId,review,reviewImg);
    }

    /**
     * 添加追评回复
     *
     * @param commentId   评论编号
     * @param reviewReply 追评回复
     * @return
     */
    @Override
    public boolean reviewReply(Integer commentId, String reviewReply) {
        return goodsCommentDao.reviewReply(commentId,reviewReply);
    }

    /**
     * 添加评论回复
     *
     * @param commentId    评论编号
     * @param commentReply 评论回复
     * @return
     */
    @Override
    public boolean commentReply(Integer commentId, String commentReply) {
        return goodsCommentDao.commentReply(commentId,commentReply);
    }
}
